﻿//==============================================================================  
//Copyright (C) 2012-2015 9UN.ORG. All rights reserved. 
//GUID：eaf50b6a-0805-4de7-909a-3b7d0b7615db
//CLR Version: 4.0.30319.18033
//Code Author：Kevin Wang
//Contact：Email(Admin@9un.org),QQ(265382 or 74344)
//Filename：RenderPluginContainer
//Namespace：RuanRang.Core
//Functions：PDFRenderProxy  
//Created by Kevin Wang at 2013/2/2 22:48:53 http://blog.9un.org
//============================================================================== 
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;

namespace RuanRang.Core
{
    /// <summary>
    /// Class PDFRenderProxy
    /// </summary>
    public class RenderPluginContainer
    {
        /// <summary>
        /// Gets or sets the assembly collection.
        /// </summary>
        /// <value>The assembly collection.</value>
        public IDictionary<RenderPluginType, Assembly> AssemblyCollection { get; protected set; }

        /// <summary>
        /// Gets or sets the type collection.
        /// </summary>
        /// <value>The type collection.</value>
        public IDictionary<string, Type> TypeCollection { get; protected set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="RenderPluginContainer" /> class.
        /// </summary>
        /// <exception cref="System.IO.IOException">程序集文件不存在，路径： + assemblyFilePath</exception>
        public RenderPluginContainer()
        {
            AssemblyCollection = new Dictionary<RenderPluginType, Assembly>();
            TypeCollection = new Dictionary<string, Type>();
        }

        /// <summary>
        /// Regesiters the specified type.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="assemblyFilePath">The assembly file path.</param>
        /// <exception cref="System.IO.IOException">程序集文件不存在，路径： + assemblyFilePath</exception>
        public void Register(RenderPluginType type, string assemblyFilePath)
        {
            if (!File.Exists(assemblyFilePath))
            {
                throw new IOException("程序集文件不存在，路径：" + assemblyFilePath);
            }
            if (AssemblyCollection.ContainsKey(type))
            {
                throw new IOException("该类型的程序集已经在容器中注册");
            }
            var sourceAssembly = Assembly.LoadFile(assemblyFilePath);
            AssemblyCollection.Add(type, sourceAssembly);
        }

        /// <summary>
        /// Resolves the specified type.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="className">Name of the class.</param>
        /// <returns>Type.</returns>
        public Type Resolve(RenderPluginType type, string className)
        {
            if (!AssemblyCollection.ContainsKey(type))
            {
                throw new Exception("该类型未在程序集容器中注册");
            }
            var sourceAssembly = AssemblyCollection[type];
            //获取类型
            Type srcType;
            if (TypeCollection.ContainsKey(className))
            {
                srcType = TypeCollection[className];
                return srcType;
            }
            //注册类型到类型集合
            srcType = sourceAssembly.GetType(className);
            TypeCollection.Add(className,srcType);
            return srcType; 
        }

        /// <summary>
        /// Resolves the specified type.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <returns>Assembly.</returns>
        public Assembly Resolve(RenderPluginType type)
        {
            return AssemblyCollection.ContainsKey(type) ? AssemblyCollection[type] : null;
        }
    }
}
